package com.example.smartcity1.service.patient;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.smartcity1.R;
import com.example.smartcity1.bean.HospitalBannerBean;
import com.example.smartcity1.bean.HospitalPageBean;
import com.example.smartcity1.my.MyLoginActivity;
import com.example.smartcity1.network.RetrofitClient;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


import static com.example.smartcity1.network.RetrofitClient.IP;
import static com.example.smartcity1.network.Utils.login;

public class HospitalPageActivity extends AppCompatActivity {

    private TextView titleView;
    private Banner banner;
    private TextView contentView;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_page);
        initView();
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("医院详情");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        Intent intent = getIntent();
        int id = intent.getIntExtra("id", 1);
        RetrofitClient.serviceService().getHospitalPageData(id).enqueue(new Callback<HospitalPageBean>() {
            @Override
            public void onResponse(Call<HospitalPageBean> call, Response<HospitalPageBean> response) {
                HospitalPageBean bean = response.body();
                if (bean != null && bean.getCode() == 200) {
                    HospitalPageBean.DataDTO data = bean.getData();
                    titleView.setText(data.getHospitalName());
                    contentView.setText(data.getBrief());
                }
            }
            @Override
            public void onFailure(Call<HospitalPageBean> call, Throwable t) {

            }
        });
        SharedPreferences user = getSharedPreferences("user",MODE_PRIVATE);
        String token = user.getString("token", "");
        RetrofitClient.serviceService().getHospitalBannerData(token,id).enqueue(new Callback<HospitalBannerBean>() {
            @Override
            public void onResponse(Call<HospitalBannerBean> call, Response<HospitalBannerBean> response) {
                HospitalBannerBean body = response.body();
                if (body != null && body.getCode() == 200) {
                    List<HospitalBannerBean.RowsDTO> rows = body.getRows();
                    banner.setAdapter(new BannerImageAdapter<HospitalBannerBean.RowsDTO>(rows) {
                        @Override
                        public void onBindView(BannerImageHolder bannerImageHolder, HospitalBannerBean.RowsDTO rowsDTO, int i, int i1) {
                            Glide.with(HospitalPageActivity.this)
                                    .load(IP + rows.get(i).getImgUrl())
                                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                    .placeholder(R.drawable.ic_baseline_cloud_download_24)
                                    .into(bannerImageHolder.imageView);
                        }
                    }).addBannerLifecycleObserver(HospitalPageActivity.this)//添加生命周期观察者
                            .setIndicator(new CircleIndicator(HospitalPageActivity.this));
                }
            }

            @Override
            public void onFailure(Call<HospitalBannerBean> call, Throwable t) {

            }
        });
        button.setOnClickListener(v -> {
            if (login == 0) {
                Toast.makeText(HospitalPageActivity.this, "尚未登录, 请登录", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(HospitalPageActivity.this, MyLoginActivity.class));
            } else {
                startActivity(new Intent(HospitalPageActivity.this,PatientListActivity.class));
            }
        });
    }

    private void initView() {
        titleView = findViewById(R.id.title);
        banner = findViewById(R.id.banner);
        contentView = findViewById(R.id.content);
        button = findViewById(R.id.button);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }
}